package com.klhappy.houyuan.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import com.klhappy.houyuan.R;
import com.klhappy.houyuan.bean.BaseJsonBean;
import com.klhappy.houyuan.constant.UrlConstant;
import com.klhappy.houyuan.dialog.AddLoctionDialog;
import com.klhappy.houyuan.interfaces.DialogInterface;
import com.klhappy.houyuan.interfaces.CallBackInterModle;
import com.klhappy.houyuan.modle.JiaYuanModle;
import com.klhappy.houyuan.utils.NotifyMethod;
import com.klhappy.houyuan.view.AbsActivity;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2015/11/13.
 */
public class AddLocationActivity extends AbsActivity implements CallBackInterModle {
    public static final String AVG = "AddLocationActivity";
    @InjectView(R.id.title)
    TextView title;
    @InjectView(R.id.name)
    EditText name;
    @InjectView(R.id.phone)
    EditText phone;
    @InjectView(R.id.loction)
    TextView loction;
    @InjectView(R.id.number)
    EditText number;

    @OnClick(R.id.click_ra)
    public void click_ra(){
        finish();
    }
    @OnClick(R.id.loction)
    public void loction(){
        AddLoctionDialog dialog = new AddLoctionDialog(this);
        dialog.setDialog();
        dialog.Show();
    }
    @OnClick(R.id.add_submit)
    public void add_submit(){
        String nameX = name.getText().toString();
        String phoneX =phone.getText().toString();
        String loctionX = loction.getText().toString();
        String numberX = number.getText().toString();
        if (!TextUtils.isEmpty(nameX) && !TextUtils.isEmpty(phoneX)
                && !TextUtils.isEmpty(loctionX) && !TextUtils.isEmpty(numberX)){
            modle.setAddress(nameX ,phoneX ,loctionX ,numberX);
            showDialog("请稍等...");
        }
    }

    private JiaYuanModle modle;
    private Intent intent;
    private String address;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_location);
        ButterKnife.inject(this);
        title.setText("添加地址");
        intent = new Intent();
        modle = new JiaYuanModle(this);
        modle.setCallBackInterModle(this);

        NotifyMethod.getInstance().addObserver(AVG, new NotifyMethod.NotifyListener() {
            @Override
            public void notify(Object o) {
                loction.setText((String)o);
            }
        });
    }

    @Override
    public void CallBackFromModle(String url, BaseJsonBean o) {
        if (url.equals(UrlConstant.ADDADRESS_URL)){
            closeDialog();
            if (o.getCode() == 200){
                showToast(o.getMsg());
                address = loction.getText().toString() + number.getText().toString();
                intent.putExtra("address", address);
                setResult(40, intent);
                finish();
            }else {
                showToast(o.getMsg());
            }
        }
    }
}
